package com.atguigu.Bean;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class OrderBean implements WritableComparable<OrderBean> {
    //定义orderId和price变量
    private String orderId;
    private Double price;

    /**
     * 重写compareTo方法
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(OrderBean o) {
        //先对orderId进行比较，如果相同，将它们的price放一起比较，不同就不比较
        int result = this.orderId.compareTo(o.orderId);
        //进行判断
        if (result == 0) {
            int i = this.price.compareTo(o.price);
            return -i;      //返回i求取最小值，返回-i求取最大值
        }
        return result;

    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(orderId);
        out.writeDouble(price);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.orderId = in.readUTF();
        this.price = in.readDouble();
    }

    //生成get(),set()方法

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    //生成toString()方法

    @Override
    public String toString() {
        return orderId + "\t" + price;
    }
}
